<table class="table table-hover">
    <tr>
        <th>属性ID</th>
        <th>属性名称</th>
        <th>操作</th>
    </tr>
    <?php foreach($data as $v):?>
    <tr>
        <td><?php echo $v['id']?></td>
        <td><?php echo $v['name']?></td>
        <td>
            <a href="javascript:;"
               data-name="<?php echo $v['name']?>"
               data-id="<?php echo $v['id']?>"
               class="btn btn-primary btn-xs update">修改</a>

            <a href="javascript:;" data-msg="确定删除吗?" data-id="delete" data-src="<?php echo U('Admin/Goods/Attr/deleteAttr')?>" data-data="<?php echo $v['id']?>" class="btn btn-danger btn-xs _ajaxNote">删除</a>
            <a href="javascript:;" data-id="<?php echo $v['id']?>" data-name="<?php echo $v['name']?>" class="btn btn-default btn-xs load-val">查看属性值</a>
        </td>
    </tr>
    <?php endforeach;?>
</table>
<script>

    $(function(){

        //加载属性值的列表
        $('.load-val').click(function(){

            var id = $(this).data('id');

            var name = $(this).data('name');

            layer.load();
            $.post("<?php echo U('Admin/Goods/Attr/attrValLists')?>",{'id':id},function(result){

                layer.closeAll();
                if(result.code == 300)
                {

                    showMessage(result.msg,result.code,false,false,2000);
                    return false;

                }

                layer.open({
                    title: '当前属性: '+name,
                    type: 1,
                    skin: 'layui-layer-rim', //加上边框
                    area: ['50%', '80%'], //宽高
                    content: result,
                    btn: ['添加','关闭'],
                    yes: function(index,_this){
                        //添加一个属性值
                        layer.prompt(
                            {
                                title: '多个请使用空格隔开或换行',
                                formType: 2 //prompt风格，支持0-2
                            },function(name){

                                var data = {};
                                data.attr_id = id;

                                    var name = $.trim(name);

                                    data.name = name.split(/\s+/);

                                    var load = layer.load();

                                $.post('<?php echo U("Admin/Goods/Attr/createOneAttrVal")?>',{'data':data},function(result){

                                    layer.close(load);

                                    if(result.code == 200){

                                        layer.msg(result.msg,{offset:0,time:1000},function(){

                                            for(var i in result.data){


                                                $('tbody',_this)
                                                        .append('\
                                                <tr>\
                                                    <td>'+result.data[i].id+'</td>\
                                                    <td>'+result.data[i].name+'</td>\
                                                    <td>\
                                                    <a href="javascript:;"\
                                                    data-name="'+result.data[i].name+'"\
                                                    data-id="'+result.data[i].id+'"\
                                                    data-attr_id="'+id+'"\
                                                    class="btn btn-primary btn-xs update_val">修改</a>\
                                                    \
                                                    <a href="javascript:;" data-attr_id="'+id+'" data-id="'+result.data[i].id+'" class="btn btn-danger btn-xs delete_val">删除</a>\
                                                </td>\
                                                </tr> \
                                                ');

                                            }

                                        })

                                    }else{

                                        layer.msg(result.msg,{offset:0,shift:6});

                                    }

                                })

                            }
                        );


                    }

                });

            });

        });


        $('.update').click(function(){

            var _this = $(this);
            var data = {};
            data.id = _this.data('id');
            var name = _this.data('name');

            layer.prompt(
            {
                title: '您将修改属性: '+name,
                formType: 0 //prompt风格，支持0-2
            }
            ,function(val){

                data.name = val;

                var load = layer.load();
                $.post('<?php echo U("Admin/Goods/Attr/updateAttr")?>',{'data':data},function(result){

                    layer.close(load);

                    if(result.code == 200){

                        layer.msg(result.msg,{offset:0,time:1000},function(){

                            _this
                                    .parent()
                                    .prev()
                                    .text(data.name);

                        });

                    }else{

                        layer.msg(result.msg,{offset:0,shift:6});

                    }


                })

            });

        });

    });

    function ajaxRequestCallback(result,id,_this){

        showMessage(result.msg,result.code,false,false,2000);
        if(result.code == 200)
            _this
                    .parent()
                    .parent()
                    .fadeOut(200,function(){

                        $(this).remove();

                    });

    }
</script>